home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / TIME_ZON / CNAMELIS.C < prev    next >
Text File  |  1991-11-08  |  9KB  |  424 lines

  1. /* CNameList.c
  2.  *    This object is a subclass of CList.  It has the added ability to deal
  3.  *    with its listed objects by their instance variable 'name'.
  4.  */
  5.  
  6. #include "CNameList.h"
  7. #include "CListBox.h"
  8. #include <GenRoutines.h>
  9.  
  10. void
  11. CNameList::INameList()
  12. {
  13.     inherited::IList();
  14.     
  15.         /*
  16.          * There is currently no new function, and no owner
  17.          */
  18.     
  19.     newFunc = NULL;
  20.     owner = NULL;
  21. }
  22.  
  23.  
  24. void
  25. CNameList::SetNewFunc(theFunc, listOwner)
  26. CNObject * (* theFunc)();
  27. CObject * listOwner;
  28. {
  29.         /*
  30.          * The function passed in will be called when a new object needs
  31.          * to be created, and the owner object will be passed to it.
  32.          */
  33.  
  34.     newFunc = theFunc;
  35.     owner = listOwner;
  36. }
  37.  
  38.  
  39. int
  40. CNameList::Insert(theObject)
  41. CNObject *theObject;
  42. {
  43.     CNObject *otherObj;
  44.     long place;
  45.     
  46.         /*
  47.          * Find the first name which is greater than the input name:
  48.          */
  49.     
  50.     otherObj = (CNObject *) FirstSuccess1(&NG,(long)theObject->name);
  51.     
  52.         /*
  53.          * If there isn't one, add this at the end. Otherwise, insert
  54.          * it at this location, to keep things in order by name.
  55.          */
  56.          
  57.     if (otherObj == NULL)
  58.         Append(theObject);
  59.     else {
  60.         place = FindIndex(otherObj);
  61.         InsertAt(theObject,place);
  62.     }
  63.     
  64.         /*
  65.          * Return the index of the new location
  66.          */
  67.     
  68.     return(FindIndex(theObject));
  69. }
  70.  
  71.  
  72. static Boolean
  73. NG(theObject,theName)
  74. CNObject *theObject;
  75. long theName;
  76. {
  77.         /*
  78.          * Is the name of this object greater than the input name?
  79.          */
  80.  
  81.     return(theObject->NameGreater((char *) theName));
  82. }
  83.  
  84.  
  85. CNObject *
  86. CNameList::FindName(theName)
  87. char *theName;
  88. {
  89.     CNObject *theObject;
  90.     Boolean NE();
  91.     
  92.         /*
  93.          * Find the first successful name match
  94.          */
  95.     
  96.     return((CNObject *) FirstSuccess1(&NE, (long) theName));
  97. }
  98.  
  99.  
  100. static Boolean
  101. NE(theObject,theName)
  102. CNObject *theObject;
  103. long theName;
  104. {
  105.         /*
  106.          * Is this object name equal to the input name?
  107.          */
  108.  
  109.     return(theObject->NameEquals((char *) theName));
  110. }
  111.  
  112.  
  113. CNObject    *
  114. CNameList::SelectName()
  115. {
  116.     #define CGEDLG 302
  117.     #define SELECT 1
  118.     #define DONE 2
  119.     
  120.     DialogPtr thedlg;
  121.     GrafPtr save;
  122.     int which, res, ect, ict, where;
  123.     short thetype;
  124.     Handle theitem;
  125.     Rect thebox, db;
  126.     CNObject *theObject;
  127.     CListBox *theLBox;
  128.     CNameList *theLBList;
  129.     char result[256];
  130.     
  131.         /*
  132.          * This routine presents a dialog from which the name of one
  133.          * of the items in the list can be selected.
  134.          */
  135.     
  136.     
  137.         /* 
  138.          * First, get a dialog
  139.          */
  140.          
  141.     ParamText("\pChoose","","","");
  142.     thedlg = GetNewDialog(CGEDLG,NULL,(WindowPtr) -1);
  143.     install_useritem(thedlg,(short) 3,(ProcPtr) &outline_ok);
  144.     
  145.         /*
  146.          * Make a list for the CListBoxes in this dialog. Actually there
  147.          * is only one, but the CListBox class is designed to be able
  148.          * to handle more than one.
  149.          */
  150.     
  151.     theLBList = new(CNameList);
  152.     theLBList->INameList();
  153.     
  154.         /*
  155.          * Obtain the number of items in the list, and make up the
  156.          * CListBox with that many cells. Make it item 4 in the dialog
  157.          * since that's how the dialog is defined.
  158.          */
  159.     
  160.     ect = GetNumItems();
  161.     GetDItem(thedlg,4,&thetype,&theitem,&thebox);
  162.     theLBox = new(CListBox);
  163.     theLBox->IListBox((WindowPtr) thedlg,&thebox,ect, 4);
  164.     install_useritem(thedlg,(short) 4,(ProcPtr) &draw_list);
  165.     
  166.         /*
  167.          * Insert the names of each named object into the CListBox,
  168.          * in order
  169.          */
  170.     
  171.     for(ict = 1;ict <= ect; ict++) {
  172.         theObject = (CNObject *) NthItem(ict);
  173.         theLBox->SetCell(ict,theObject->name);
  174.     }
  175.     
  176.         /*
  177.          * Add this CListBox to the list of CListBoxes, even though
  178.          * it's the only one. Also, make sure that the list of
  179.          * CListBoxes is available to the dialog when needed.
  180.          */
  181.          
  182.     theLBList->Append(theLBox);
  183.     SetWRefCon((WindowPtr) thedlg,(long) theLBList);
  184.         
  185.         /*
  186.          * I'm not sure why all this is necessary, but it seems to be...
  187.          */
  188.     
  189.     GetPort(&save);
  190.     SelectWindow(thedlg);
  191.     ShowWindow(thedlg);
  192.     SetPort(thedlg);
  193.     
  194.         /*
  195.          * Here we go! Notice that there is a filter routine (check_list in
  196.          * GenRoutines.c) which handles the action for the listbox.
  197.          */
  198.     
  199.     theObject = NULL;
  200.     which = 0;
  201.     while ((which != SELECT) && (which != DONE)) {
  202.         ModalDialog(&check_list,&which);
  203.         
  204.             /*
  205.              * Okay, we got one, unless the user selected nothing, in
  206.              * which case we'll send back NULL
  207.              */
  208.         
  209.         if (which == SELECT) {
  210.             theLBox->GetCell(result);
  211.             if (result[0] != '\0')
  212.                 theObject = FindName(result);
  213.         }
  214.     }
  215.         
  216.         /*
  217.          * Dont' need the listbox stuff anymore
  218.          */
  219.         
  220.     theLBox->Dispose();
  221.     theLBList->Dispose();
  222.     
  223.         /*
  224.          * Don't need the dialog any more
  225.          */
  226.     
  227.     DisposDialog(thedlg);
  228.     SetPort(save);
  229.     
  230.     return(theObject);
  231. }
  232.  
  233.  
  234. Boolean
  235. CNameList::Edit()
  236. {
  237.     #define CELDLG 301
  238.     #define EDIT 1
  239.     #define CANCEL 2
  240.     #define NEW 5
  241.     #define DELETE 6
  242.     
  243.     DialogPtr thedlg;
  244.     GrafPtr save;
  245.     int which, res, ect, ict, where;
  246.     short thetype;
  247.     Handle theitem;
  248.     Rect thebox, db;
  249.     CNObject *theObj;
  250.     CListBox *theLBox;
  251.     CList *theLBList;
  252.     char result[256];
  253.     Boolean modified = FALSE;
  254.     
  255.         /*
  256.          * This dialog allows the user to add, remove, and edit
  257.          * individual name items in the CNameList, even though the
  258.          * CNameList doesn't know anything about them except that they
  259.          * have names.
  260.          */
  261.          
  262.          /*
  263.           * First, get the dialog
  264.           */
  265.     
  266.     thedlg = GetNewDialog(CELDLG,NULL,(WindowPtr) -1);
  267.     install_useritem(thedlg,(short) 3,(ProcPtr) &outline_ok);
  268.     
  269.         /*
  270.          * The CListBox class allows multiple listboxes in one dialog,
  271.          * so we'll need a new list for them, even though we'll only be
  272.          * using one.
  273.          */
  274.     
  275.     theLBList = new(CList);
  276.     theLBList->IList();
  277.     
  278.         /*
  279.          * Get the number of items in our list, and put the newly made
  280.          * listbox into the dialog at position 4 (since that's where
  281.          * we said it would be in the dialog definition)
  282.          */
  283.     
  284.     ect = GetNumItems();
  285.     GetDItem(thedlg,4,&thetype,&theitem,&thebox);
  286.     theLBox = new(CListBox);
  287.     theLBox->IListBox((WindowPtr) thedlg,&thebox,ect,4);
  288.     install_useritem(thedlg,(short) 4,(ProcPtr) &draw_list);
  289.     
  290.         /*
  291.          * Load up the cells with the names of the objects in the
  292.          * list in order by name.
  293.          */
  294.     
  295.     for(ict = 1;ict <= ect; ict++) {
  296.         theObj = (CNObject *) NthItem(ict);
  297.         theLBox->SetCell(ict,theObj->name);
  298.     }
  299.     
  300.         /*
  301.          * Add this listbox to the list of listboxes, even though it
  302.          * will be the only one. Then save the list of listboxes in the
  303.          * dialog so it will be available when needed.
  304.          */
  305.          
  306.     theLBList->Append(theLBox);
  307.     SetWRefCon((WindowPtr) thedlg,(long) theLBList);
  308.  
  309.         /*
  310.          * See my note for SelectName.
  311.          */
  312.     
  313.     GetPort(&save);
  314.     SelectWindow(thedlg);
  315.     ShowWindow(thedlg);
  316.     SetPort(thedlg);
  317.     
  318.         /*
  319.          * Here we go! Note that there is a filter named check_list for
  320.          * the ModalDialog call, which can be found in GenRoutines.
  321.          */
  322.     
  323.     which = NEW;
  324.     while (which != CANCEL) {
  325.     
  326.             /*
  327.              * We reset the param text just in case the editing of the
  328.              * individual objects might have re-done it
  329.              */
  330.              
  331.         ParamText("\pEdit Time Zones","","","");
  332.     
  333.         ModalDialog(&check_list,&which);
  334.         
  335.         switch (which) {
  336.         
  337.             case NEW:
  338.             
  339.                     /*
  340.                      * So okay, we make a new one by calling the
  341.                      * function passed in by the real owner of the
  342.                      * CNameList. Then we call the object's own edit
  343.                      * routine to edit itself, then insert it according
  344.                      * to the name.
  345.                      */
  346.                 theObj = (CNObject *) (*newFunc)(owner);
  347.                 theObj->Edit();
  348.                 where = Insert(theObj);
  349.                 theLBox->InsertCell(where,theObj->name);
  350.                 modified = TRUE;
  351.                 break;
  352.                 
  353.             case CANCEL:
  354.                 break;
  355.                 
  356.             case EDIT:
  357.             case DELETE:
  358.             
  359.                     /*
  360.                      * Try to find the selected cell, and if found,
  361.                      * edit or delete, depending on the user's choice
  362.                      */
  363.                      
  364.                 theLBox->GetCell(result);
  365.                 if (result[0] != '\0') {
  366.                     theObj = (CNObject *) FindName(result);
  367.                     if (theObj != NULL) {
  368.                         if (which == EDIT) {
  369.                         
  370.                                 /*
  371.                                  * Okay, call the thing to edit itself,
  372.                                  * and then replace it in the list in
  373.                                  * case its name was changed
  374.                                  */
  375.                                  
  376.                             theObj->Edit();
  377.                             where = FindIndex(theObj);
  378.                             theLBox->RemoveCell(where);
  379.                             Remove(theObj);
  380.                             where = Insert(theObj);
  381.                             theLBox->InsertCell(where,theObj->name);
  382.                             modified = TRUE;
  383.                         }
  384.                         else { /* it must be a DELETE */
  385.                         
  386.                                 /* Okay, find it and remove it from
  387.                                  * the listbox, and from the CNameList
  388.                                  * as well. DisposeComplete is a CNObject
  389.                                  * call which gets rid of everything about
  390.                                  * the object. See CZone for the reason.
  391.                                  */
  392.                                  
  393.                             where = FindIndex(theObj);
  394.                             theLBox->RemoveCell(where);
  395.                             Remove(theObj);
  396.                             theObj->DisposeComplete();
  397.                             modified = TRUE;
  398.                         }
  399.                     }
  400.                 }
  401.                 break;
  402.         }
  403.     }
  404.     
  405.         /*
  406.          * Don't need this stuff any more
  407.          */
  408.          
  409.     theLBox->Dispose();
  410.     theLBList->Dispose();
  411.     
  412.         /*
  413.          * Or this
  414.          */
  415.     
  416.     DisposDialog(thedlg);
  417.     
  418.     SetPort(save);
  419.     
  420.     return(modified);
  421. }
  422.  
  423.  
  424.